﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace effects
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Fading : Window
    {
        private bool fadingOutFadeToGone = false;

        public Fading()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FadeOutControl(lblFade, true);
        }

        void FadeOutControl(FrameworkElement control, bool fadeBackIn)
        {
            // Inspired from: http://msdn.microsoft.com/en-us/library/ms742557.aspx

            // Create a StoryBoard for the Animation
            // These basically orchestrate the animation.
            var storyboard = new Storyboard();
            storyboard.Completed += new EventHandler(storyboard_Completed);

            // Create the Duration for the Animation
            var duration = new TimeSpan(0, 0, 1);

            // Create the Animation - since we are adjusting the Opacity property
            // we need an animation to adjust a double value.
            var animation = new DoubleAnimation()
            {
                From = fadingOutFadeToGone ? 1.0 : 0,
                To = fadingOutFadeToGone ? 0 : 1.0,
                Duration = duration, /* Set to Duration Specified */
                AutoReverse = fadeBackIn
            };

            // Here we Bind the Animation to the Control & the Property we want to Animate.
            Storyboard.SetTargetName(animation, control.Name);
            Storyboard.SetTargetProperty(animation, new PropertyPath(OpacityProperty));
            // Add the Animation to the Storyboard since it needs it!
            storyboard.Children.Add(animation);

            // MAKE IT SO!
            storyboard.Begin(this);
        }

        void storyboard_Completed(object sender, EventArgs e)
        {
            if (fadingOutFadeToGone)
                FadeToGonePanel.Children.Remove(lblFadeToGone);
        }

        private void btnFadeOutHide_Click(object sender, RoutedEventArgs e)
        {
            fadingOutFadeToGone = !fadingOutFadeToGone;

            if (!fadingOutFadeToGone)
                FadeToGonePanel.Children.Insert(0, lblFadeToGone);

            FadeOutControl(lblFadeToGone, false);
        }
    }
}
